package Java语言.深拷贝;

public class DeepCopy {
    public static void main(String[] args) {
        A a1 = new A(1,2), a2 = new A(2,3);
        A.deepSwapBUseFunc(a1, a2);
        System.out.println(a1.getC());
        System.out.println(a2.getC());
        System.out.println(a1.getC());
        System.out.println(a2.getC());
    }
}

class A {
    public A(int b, int c) {
        this.b = b;
        this.c = c;
    }

    public A(A a) {
        this.b = a.b;
        this.c = a.c;
    }

    public void copyFrom(A a) {
        this.b = a.b;
    }

    public int getB() {
        return b;
    }

    static void deepSwapBUseFunc(A a1, A a2) {//不一定要全交换
        A b = new A(a1);
        a1.copyFrom(a2);
        a2.copyFrom(b);
    }

    static void noSwap(A a1, A a2) {
        A b = a1;
        a1 = a2;
        a2 = b;
    }

    public int getC() {
        return c;
    }

    /*    static void  deepSwap(交换.A a1,交换.A a2) {//坏方法，侵犯了访问权
            交换.A b=new 交换.A(a1.b);
            a1.b=a2.b;
            a2.b=b.b;
        }*/
    private int c;
    private int b;
}